package com.example.mediamoudle.crop;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import com.android.baselibrary.utils.ScreenUtils;
import com.example.mediamoudle.R;

/**
 * @author: Mr.You
 * @create: 2022-11-25 13:22
 * @description:
 **/
public class ThumbnailProgressView extends View {

    private int mWidth;
    private int mHeight;
    private Paint mPaint;
    private Paint mPaintText;
    private Paint mPaintBack;
    private RectF rectF;
    private RectF rectF2;
    private int rectWidth;
    private Bitmap bitmapLeft;
    private Bitmap bitmapRight;
    private OnScrollBorderListener onScrollBorderListener;
    private int minPx;

    private String textShow="3.0s";

    private float downX;
    private boolean scrollLeft;
    private boolean scrollRight;

    public int maxLeft = ScreenUtils.getScreenWidth(getContext(), false) / 2 - rectWidth;
    public int maxRight = ScreenUtils.getScreenWidth(getContext(), false) / 2 - rectWidth;

    public int getMaxLeft() {
        return maxLeft;
    }

    public void setMaxLeft(int maxLeft) {
        this.maxLeft = maxLeft;
    }

    public int getMaxRight() {
        return maxRight;
    }

    public void setMaxRight(int maxRight) {
        this.maxRight = maxRight;
    }

    public void setTextShow(String textShow){
        this.textShow = textShow;
        invalidate();
    }

    public ThumbnailProgressView(Context context) {
        super(context);
        init();
    }

    public ThumbnailProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ThumbnailProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

        rectF = new RectF();
        rectF2 = new RectF();

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        int dp5 = (int) getResources().getDimension(R.dimen.dp_5);
        mPaint.setStrokeWidth(dp5);

        mPaintText = new Paint();
        mPaintText.setAntiAlias(true);
        mPaintText.setStrokeWidth(dp5);
        mPaintText.setColor(Color.parseColor("#ffffff"));
        mPaintText.setTextSize(20f);

        mPaintBack = new Paint();
        mPaintBack.setAntiAlias(true);
        mPaintBack.setStrokeWidth(dp5);
        mPaintBack.setColor(Color.parseColor("#99111111"));
        mPaintText.setTextSize(20f);

        bitmapLeft = BitmapFactory.decodeResource(getResources(), R.drawable.ic_short_video_film_left);
        bitmapRight = BitmapFactory.decodeResource(getResources(), R.drawable.ic_short_video_film_right);

        rectWidth = (int) getResources().getDimension(R.dimen.dp_10);
    }

    public int getMinPx() {
        return minPx;
    }

    public void setMinInterval(int minPx) {
        if (mWidth > 0 && minPx > mWidth) {
            minPx = mWidth;
        }
        this.minPx = minPx;
    }

    public void setWidth(int width) {
        mWidth = width;
        invalidate();
    }

    public interface OnScrollBorderListener {
        void OnScrollBorder(float start, float end);

        void onScrollStateChange();
    }

    public void setOnScrollBorderListener(OnScrollBorderListener listener) {
        this.onScrollBorderListener = listener;
    }

    public void setLeftBitmap(float left) {
        this.rectF.left = left;
        rectF.right = left - rectWidth;
        invalidate();
    }

    public void setBitmapScroll(float scroll) {
        this.rectF.left = this.rectF.left - scroll;
        rectF.right = rectF.right - scroll;
        this.rectF2.right = this.rectF2.right - scroll;
        this.rectF2.left = this.rectF2.left - scroll;
        invalidate();
    }

    public void setRightBitmap(float right) {
        this.rectF2.right = right;
        this.rectF2.left = right - rectWidth;
        invalidate();
    }

    public float getLeftInterval() {
        return rectF.right;
    }

    public float getRightInterval() {
        return rectF2.left;
    }

    public float getTotalWidth() {
        return mWidth;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        mHeight = getHeight();
        rectF.left = maxLeft - rectWidth;
        rectF.top = 0;
        rectF.right = maxLeft;
        rectF.bottom = mHeight;

        rectF2.left = rectF.right + mWidth;
        ;
        rectF2.top = 0;
        rectF2.right = rectF.right + mWidth + rectWidth;
        rectF2.bottom = mHeight;

    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {

        move(event);
        return scrollLeft || scrollRight;
    }

    boolean scrollChange;

    private boolean move(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                if (downX > rectF.left - rectWidth / 2 && downX < rectF.right + rectWidth / 2) {
                    scrollLeft = true;
                }
                if (downX > rectF2.left - rectWidth / 2 && downX < rectF2.right + rectWidth / 2) {
                    scrollRight = true;
                }
                break;
            case MotionEvent.ACTION_MOVE:

                float moveX = event.getX();

                float scrollX = moveX - downX;
                if (scrollLeft) {
                    rectF.left = rectF.left + scrollX;
                    rectF.right = rectF.right + scrollX;
                    if (rectF.left + rectWidth < maxLeft) {
                        rectF.left = maxLeft - rectWidth;
                        rectF.right = maxLeft;
                    }

                    if (rectF.right > rectF2.left - minPx) {
                        rectF.right = rectF2.left - minPx;
                        rectF.left = rectF.right - rectWidth;
                    }
                    scrollChange = true;
                    invalidate();
                } else if (scrollRight) {
                    rectF2.left = rectF2.left + scrollX;
                    rectF2.right = rectF2.right + scrollX;

                    if (rectF2.right - rectWidth > maxRight) {
                        rectF2.right = maxRight + rectWidth;
                        rectF2.left = maxRight;
                    }

                    if (rectF2.left < rectF.right + minPx) {
                        rectF2.left = rectF.right + minPx;
                        rectF2.right = rectF2.left + rectWidth;
                    }
                    scrollChange = true;
                    invalidate();

                }

                if (onScrollBorderListener != null) {
                    onScrollBorderListener.OnScrollBorder(rectF.left, rectF2.right);
                }


                downX = moveX;
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                downX = 0;
                scrollLeft = false;
                scrollRight = false;
                if (scrollChange && onScrollBorderListener != null) {
                    onScrollBorderListener.onScrollStateChange();
                }
                scrollChange = false;
                break;

        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setColor(Color.parseColor("#ffffff"));


        Rect rect = new Rect();
        rect.left = (int) rectF.left;
        rect.top = (int) rectF.top;
        rect.right = (int) rectF.right;
        rect.bottom = (int) rectF.bottom;
        canvas.drawBitmap(bitmapLeft, null, rectF, mPaint);

        Rect rect2 = new Rect();
        rect2.left = (int) rectF2.left;
        rect2.top = (int) rectF2.top;
        rect2.right = (int) rectF2.right;
        rect2.bottom = (int) rectF2.bottom;
        canvas.drawBitmap(bitmapRight, null, rectF2, mPaint);


        canvas.drawLine(rectF.right, 0, rectF2.left, 0, mPaint);
        canvas.drawLine(rectF.right, mHeight, rectF2.left, mHeight, mPaint);

        mPaint.setColor(Color.parseColor("#99000000"));

        Point textSize = new Point();
        Rect bounds = new Rect();
        mPaintText.getTextBounds(textShow, 0, textShow.length(), bounds);
        textSize.x = bounds.width();
        textSize.y = bounds.height();

        RectF rectFt = new RectF();
        rectFt.left = rectF.right + textSize.x/2 - textSize.x/4 ;
        rectFt.top = 30 -  textSize.y / 2f-10 ;
        rectFt.right = rectF.right + textSize.x + textSize.x/2+textSize.x/4;
        rectFt.bottom = 30 + textSize.y / 2f + 10;
        canvas.drawRect(rectFt, mPaintBack);

        canvas.drawText(textShow, rectF.right +  textSize.x / 2, 30 + textSize.y / 2f, mPaintText);



        RectF rectF3 = new RectF();
        rectF3.left = 0;
        rectF3.top = 0;
        rectF3.right = rectF.left;
        rectF3.bottom = mHeight;
        canvas.drawRect(rectF3, mPaint);

        RectF rectF4 = new RectF();
        rectF4.left = rectF2.right;
        rectF4.top = 0;
        rectF4.right = ScreenUtils.getScreenWidth(getContext(),false);
        rectF4.bottom = mHeight;
        canvas.drawRect(rectF4, mPaint);
    }


}
